import Vue from 'vue'
import Router from 'vue-router'
import layout from '@/layout/index.vue'
Vue.use(Router)

export const routes = [
  {
    path: '',
    component: layout,
    redirect: 'index',
    alwaysShow:1,
    hidden:0,
    menuName:'首页',
    children: [
      {
        path: 'index',
        component: (resolve) => require(['@/views/index'], resolve),
        name: '首页',
        meta: { title: '首页', noCache: true, affix: true }
      },
      {
        path: 'note',
        component: (resolve) => require(['@/views/note'], resolve),
        name: '心情随笔',
        meta: { title: '心情随笔', noCache: true, affix: true }
      },
      {
        path: 'memory',
        component: (resolve) => require(['@/views/memory'], resolve),
        name: '韶华追忆',
        meta: { title: '韶华追忆',noCache: true, affix: true }
      },
      {
        path: 'works',
        component: (resolve) => require(['@/views/works'], resolve),
        name: '我的作品',
        meta: { title: '我的作品', noCache: true, affix: true }
      },
      {
        path: 'message',
        component: (resolve) => require(['@/views/message'], resolve),
        name: 'Blog留言',
        meta: { title: 'Blog留言', noCache: true, affix: true }
      },
      {
        path: 'about',
        component: (resolve) => require(['@/views/about'], resolve),
        name: '关于我',
        meta: { title: '关于我', noCache: true, affix: true }
      },
      {
        path: 'detail',
        component: (resolve) => require(['@/views/detail'], resolve),
        name: '详情',
        meta: { title: '详情', noCache: true, affix: true }
      }
    ]
  },
  // },
  // {
  //   path: '/login',
  //   name: 'login',
  //   component: () => import('@/views/login')
  // }
]

const router =  new Router({
  // mode: 'history',  //去掉url中的#
  scrollBehavior: () => ({ y: 0 }),
  routes: routes
})
router.beforeEach(async (to, from,next) => {
  updateTitle(to);
  next() // hack方法 确保addRoutes已完成
})
/**
 * 更新浏览器标题
 * @param route 路由信息
 */
 function updateTitle(route) {
  if (route) {
    const names = [];
    names.push('小江个人博客')
    if (route.meta?.title) {
      names.push(route.meta.title);
    }
    document.title = names.join(' - ');
  }
}


  export default router